﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GNF.Geo;

namespace GNF.WPFControls
{
    /// <summary>
    /// Interaction logic for ShowGoogleMaps.xaml
    /// </summary>
    public partial class ShowGoogleMaps : UserControl
    {
        #region const members

        /// <summary>
        /// the start string for the google maps url
        /// </summary>
        protected const string GoogleMapsStart = "http://maps.google.com/maps?hl=en";

       
        /// <summary>
        /// the defualt zoom value in google maps
        /// </summary>
        protected const int defualtZoom = 9;
        
        #endregion

        #region private members
        /// <summary>
        /// the GPS DATA of the control
        /// </summary>
        protected GPSData _data;

        /// <summary>
        /// the zoom level in the map
        /// </summary>
        protected int? _zoomLevel;

        #endregion

        #region properties
        
        /// <summary>
        /// the zoom level of the map
        /// the setter will get the zoom level only if the zoom level is valid ( 20 > zoom > 0)
        /// </summary>
        public int ZoomLevel
        {
            get
            {
                return _zoomLevel ?? defualtZoom;
            }
            set
            {
                if (value < 21 && value > 0)
                {
                    _zoomLevel = value;
                }
            }
        }

        /// <summary>
        /// the current Uri including the data from the gpd data
        /// </summary>
        public Uri CurrentUri
        {
            get
            {
                //http://maps.google.com/maps?ll=60.487160,-104.266577&spn=0.004059,0.005240&hl=en&z=10
                //first is latitude
                //second is longitude
                return new Uri(string.Format("{0}&ll={1},{2}&z={3}",GoogleMapsStart,_data.Latitude,_data.Longitude,ZoomLevel));
            }
        }

        

        #endregion

        #region Construction and init

        /// <summary>
        /// the defualt constructor uses israel as the defualt place
        /// </summary>
        public ShowGoogleMaps() : this(new GPSData(36,31.5,0,0)) { }
        //(רוחב,אורך)

        /// <summary>
        /// construct the control with the location as data
        /// </summary>
        /// <param name="data">the data that the control will show</param>
        public ShowGoogleMaps(GPSData data) : this(data,defualtZoom) { }

        /// <summary>
        /// construct the control with the location as data and a zoom level
        /// </summary>
        /// <param name="data">the data that the control will show</param>
        /// <param name="zoomLevel"></param>
        public ShowGoogleMaps(GPSData data,int zoomLevel)
        {
            InitializeComponent();
            _data = data;
            ZoomLevel = zoomLevel;
            //_Browser.Navigate(CurrentUri);      
            RefreshControl();
        }
        
        /// <summary>
        /// change the source to be according to the current GPS Data,
        /// and update the Latitude and Longitute labels value
        /// </summary>
        protected void RefreshControl()
        {
            this._Browser.Source = CurrentUri;
            this._Uri.Content = CurrentUri;
            this.Latitudevaluelabel.Content = _data.Latitude;
            this.Longitudevaluelabel.Content = _data.Longitude;
        }


        
        #endregion

        #region event handlers

        /// <summary>
        /// change the current Longitude by +0.5
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void AddLong_Click(object sender, RoutedEventArgs e)
        {
            this._data.Longitude += 0.5;
            this.RefreshControl();
            e.Handled = true;
        }

        /// <summary>
        /// change the current Longitude by -0.5
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void RedLong_Click(object sender, RoutedEventArgs e)
        {
         
            this._data.Longitude -= 0.5;
            this.RefreshControl(); e.Handled = true;
        }

        /// <summary>
        /// change the current Latitude by +0.5
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void AddLat_Click(object sender, RoutedEventArgs e)
        {
           
            this._data.Latitude+= 0.5;
            this.RefreshControl();
            e.Handled = true;
        }

        /// <summary>
        /// change the current Latitude by -0.5
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void RedLat_Click(object sender, RoutedEventArgs e)
        {

            this._data.Latitude-= 0.5;
            this.RefreshControl();
            e.Handled = true;
        }

        /// <summary>
        /// change the current Zoom by +1
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void ZoomPlus(object sender, RoutedEventArgs e)
        {
            this.ZoomLevel += 1;
            this.RefreshControl();
            e.Handled = true;
        }

        /// <summary>
        /// change the current Zoom by -1
        /// and then refresh the control with the RefreshControl method
        /// </summary>
        private void ZoomMinus(object sender, RoutedEventArgs e)
        {
            this.ZoomLevel -= 1;
            this.RefreshControl();
            e.Handled = true;
        }

        #endregion

        #region methods

        #endregion


    }
}
